package com.imooc.controller;

import com.google.common.collect.Lists;
import com.imooc.enums.YesOrNo;
import com.imooc.pojo.Carousel;
import com.imooc.service.CarouselService;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.JsonUtils;
import com.imooc.utils.RedisOperator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Administrator
 */
@Api(value = "首页展示", tags = {"用于首页展示功能接口"})
@RestController
@RequestMapping({"/index"})
public class IndexController {



    @Autowired
    private CarouselService carouselService;

    @Autowired
    private RedisOperator redisOperator;

    @ApiOperation(value = "查询所有可展示的轮播图", notes = "查询所有可展示的轮播图", httpMethod = "GET")
    @GetMapping("/listCarousel")
    public IMOOCJSONResult queryUserNameIsExist() {
        List<Carousel> list;
        String listCarousel = redisOperator.get("listCarousel");
        if (StringUtils.isBlank(listCarousel)) {
            // 查出数据加入redis缓存
            list = carouselService.listCarousel(YesOrNo.YES.type);
            redisOperator.set("listCarousel", JsonUtils.objectToJson(list));
        } else {
            // 将redis中的缓存取出并展示
            list = JsonUtils.jsonToList(listCarousel, Carousel.class);
        }
        return IMOOCJSONResult.ok(list);
    }

}